package com.icode.web.socket.standard.inheritance;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * User: alexz
 * Date: 14-7-31
 * Time: 下午5:29
 */
@ServerEndpoint("/chat")
public class ChatEndpoint extends Endpoint {

    @Override
    public void onOpen(Session session, EndpointConfig config) {
        final RemoteEndpoint.Basic basicRemote = session.getBasicRemote();
        session.addMessageHandler(new MessageHandler.Partial<String>() {

            @Override
            public void onMessage(String messagePart, boolean last) {
                try {
                    basicRemote.sendText(messagePart);
                } catch (IOException e) {
                    throw new IllegalStateException("fail to send message!");
                }
            }
        });
    }
}
